package com.fairytask.utils
{
	import flash.geom.Point;

	public class Vec2D extends Object
	{
		public var x:Number=0;
		public var y:Number=0;
		
		public function Vec2D(pX:Number=0, pY:Number=0)
		{
			super();
			x = pX;
			y = pY;
		}
		
		public function cross(pVal:Vec2D):Number
		{
			return x * pVal.y - y * pVal.x;
		}
		
		public function divVec(pVal:Vec2D):Vec2D
		{
			return new Vec2D(x / pVal.x, y / pVal.y);
		}
		
		public function toString():String
		{
			return x.toString() + " " + y.toString();
		}
		
		public function dot(pVal:Vec2D):Number
		{
			return x * pVal.x + y * pVal.y;
		}
		
		public function mulN(pVal:Number):void
		{
			x = x * pVal;
			y = y * pVal;
		}
		
		public function addN(pVal:Number):void
		{
			x = x + pVal;
			y = y + pVal;
		}
		
		public function subN(pVal:Number):void
		{
			x = x - pVal;
			y = y - pVal;
		}
		
		public function get point():flash.geom.Point
		{
			return new Point(x, y);
		}
		
		public function isLeft(pVal:Vec2D):Boolean
		{
			return cross(pVal) >= 0 ? true : false;
		}
		
		public function mulV(pVal:Vec2D):void
		{
			x = x * pVal.x;
			y = y * pVal.y;
		}
		
		public function get angle():Number
		{
			var loc1:Number=Math.atan2(y, x) * 180 / Math.PI;
			if (loc1 < 0) 
			{
				loc1 = loc1 + 360;
			}
			return loc1;
		}
		
		public function get vec():Vec2D
		{
			return new Vec2D(x, y);
		}
		
		public function addV(pVal:Vec2D):void
		{
			x = x + pVal.x;
			y = y + pVal.y;
		}
		
		public function subV(pVal:Vec2D):void
		{
			x = x - pVal.x;
			y = y - pVal.y;
		}
		
		public function project(pVal:Vec2D):Vec2D
		{
			var loc3:Number=dot(pVal) / pVal.dot(pVal);
			return pVal.mulNum(loc3);
		}
		
		public function get len():Number
		{
			return Math.sqrt(x * x + y * y);
		}
		
		public function divN(pVal:Number):void
		{
			x = x / pVal;
			y = y / pVal;
		}
		
		public function set point(pVal:flash.geom.Point):void
		{
			x = pVal.x;
			y = pVal.y;
		}
		
		public function mulNum(pVal:Number):Vec2D
		{
			return new Vec2D(x * pVal, y * pVal);
		}
		
		public function getAngle(pVal:Vec2D):Number
		{
			var loc1:Number=nDot(pVal);
			return Math.acos(loc1) * 180 / Math.PI;
		}
		
		public function divV(pVal:Vec2D):void
		{
			x = x / pVal.x;
			y = y / pVal.y;
		}
		
		public function subNum(pVal:Number):Vec2D
		{
			return new Vec2D(x - pVal, y - pVal);
		}
		
		public function get normal():Vec2D
		{
			return new Vec2D(-y, x);
		}
		
		public function set vec(pVal:Vec2D):void
		{
			x = pVal.x;
			y = pVal.y;
		}
		
		public function get reverse():Vec2D
		{
			return new Vec2D(-x, -y);
		}
		
		public function addNum(pVal:Number):Vec2D
		{
			return new Vec2D(x + pVal, y + pVal);
		}
		
		/**
		 * 设置角度 
		 * @param pVal
		 * 
		 */
		public function set angle(pAngle:Number):void
		{
			var loc1:Number=pAngle * Math.PI / 180;
			x = Math.cos(loc1);
			y = Math.sin(loc1);
		}
		
		public function normalize():void
		{
			var tmpVec:Vec2D=new Vec2D(0, 0);
			tmpVec.x = x / len;
			tmpVec.y = y / len;
			vec = tmpVec;
		}
		
		/**
		 * 取得二级Vector 
		 * @param pVec
		 * @return 
		 * 
		 */
		public function subVec(pVec:Vec2D):Vec2D
		{
			return new Vec2D(x - pVec.x, y - pVec.y);
		}
		
		public function divNum(pVal:Number):Vec2D
		{
			return new Vec2D(x / pVal, y / pVal);
		}
		
		public function mulVec(pVal:Vec2D):Vec2D
		{
			return new Vec2D(x * pVal.x, y * pVal.y);
		}
		
		public function addVec(pVal:Vec2D):Vec2D
		{
			return new Vec2D(x + pVal.x, y + pVal.y);
		}
		
		public function nDot(pVal:Vec2D):Number
		{
			return normalized.x * pVal.normalized.x + normalized.y * pVal.normalized.y;
		}
		
		public function get len2():Number
		{
			return x * x + y * y;
		}
		
		public function get normalized():Vec2D
		{
			var loc2:Vec2D=new Vec2D(0, 0);
			loc2.x = x / len;
			loc2.y = y / len;
			return loc2;
		}
	}
}